package com.cslg.ac.demo.controller.user;

import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.entity.user.User;
import com.cslg.ac.demo.entity.user.team.TeamHead;
import com.cslg.ac.demo.entity.user.team.TeamInfo;
import com.cslg.ac.demo.entity.user.team.TeamProfile;
import com.cslg.ac.demo.service.user.TeamInfoService;
import com.cslg.ac.demo.util.communicate.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@PropertySource(value = {"classpath:user.properties"},encoding="utf-8")
public class TeamInfoController {

    @Autowired
    TeamInfoService teamInfoService;
    @Value("${teamBuket}")
    String teamBuket;
    @Value("${tNameLength}")
    int tNameLength;
    @Value("${natureLength}")
    int natureLength;
    @Value("${addressLength}")
    int addressLength;
    @Value("${codeLength}")
    int codeLength;
    @Value("${introLength}")
    int introLength;
    @Value("${profileLength}")
    int profileLength;
    @Value("${phoneLength}")
    int phoneLength;
    @Value("${realNameLength}")
    int realNameLength;

    @RequestMapping(value = "/user/getTeam")
    public void getTeam(@RequestParam int offset, @RequestParam int num,
                        PrintWriter out){
        if(offset < 1 || num < 0){
            ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
            return;
        }
        List<Integer> ids;
        try {
            ids = teamInfoService.getTeam(offset - 1, num);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
            return;
        }
        Map<String, Object> map = new HashMap<>(1);
        map.put("ids", ids);
        ResponseUtil.response(out, ResponseCode.REQUEST_SUCCESSED, map);
    }


    @RequestMapping(value = "/user/updateLatAndLong" ,method = RequestMethod.POST)
    public void updateLatAndLong(@RequestParam double longitude, @RequestParam double latitude,
                                 HttpSession session, PrintWriter out) {
        User user = (User) session.getAttribute("user");
        int id = user.getId();
        boolean notValid = (longitude < -90 || longitude > 90) && (latitude < 180 || latitude > 180);
        if (notValid) {
            ResponseUtil.responseCode(out, ResponseCode.PARAM_ILLEGAL);
            return;
        }
        int code;
            try {

                code = teamInfoService.updateLongAndLat(id, longitude, latitude);
            } catch (Exception e) {
                e.printStackTrace();
                ResponseUtil.responseCode(out, ResponseCode.SERVER_ERROR);
                return;
            }
            ResponseUtil.responseCode(out, code);
    }


    @RequestMapping(value = "/team/getTable")
    public void getTable(PrintWriter out){
        String url;
        try {
            url = teamInfoService.getTable();
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        Map<String,Object> param = new HashMap<>();
        param.put("url",url);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);

    }


    @RequestMapping(value = "/team/getInfo")
    public void getInfo(@RequestParam int id,HttpSession session,PrintWriter out){
        TeamProfile teamProfile;
        TeamHead teamHead;
        TeamInfo teamInfo;
        try {
            teamProfile = teamInfoService.getProfile(id);
            teamHead = teamInfoService.getHead(id);
            teamInfo = teamInfoService.getBasicInfo(id);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        Map<String,Object> param = new HashMap<>();
        param.put("teamInfo",teamInfo);
        param.put("teamHead",teamHead);
        param.put("teamProfile",teamProfile);
        ResponseUtil.response(out,ResponseCode.REQUEST_SUCCESSED,param);

    }

    @RequestMapping(value = "/team/updateProfile",method = RequestMethod.POST)
    public void updateProfile(TeamProfile teamProfile,HttpSession session,PrintWriter out){
        User user = (User)session.getAttribute("user");
        if(teamProfile == null || !teamProfile.isChangeValid(profileLength)){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        int code;
        teamProfile.setId(user.getId());
        try {
            code = teamInfoService.changeProfile(teamProfile);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        ResponseUtil.responseCode(out,code);
    }

    @RequestMapping(value = "/team/updateInfo",method = RequestMethod.POST)

    public void updateInfo(TeamInfo teamInfo, TeamHead teamHead,HttpSession session,PrintWriter out){
        User user = (User)session.getAttribute("user");
        if(teamInfo == null || teamHead == null){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        if(!teamInfo.changeValid(tNameLength,addressLength,natureLength,introLength) || !teamHead.isChangeValid(realNameLength,phoneLength)){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        int code;
        teamInfo.setId(user.getId());
        try {
            code = teamInfoService.changeInfo(teamInfo,teamHead);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        ResponseUtil.responseCode(out,code);
    }

    @RequestMapping(value = "/team/addInfo",method = RequestMethod.POST)
    public void addInfo(TeamInfo teamInfo,TeamHead teamHead, HttpSession session, PrintWriter out){
        User user = (User)session.getAttribute("user");
        boolean flag = teamInfo.addValid(tNameLength,natureLength,addressLength,codeLength,phoneLength);
        if(!flag){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        flag = teamHead.isChangeValid(realNameLength,phoneLength);
        if(!flag){
            ResponseUtil.responseCode(out,ResponseCode.PARAM_ILLEGAL);
            return;
        }
        int code;
        teamInfo.setId(user.getId());
        try {
            code = teamInfoService.addInfo(teamInfo,teamHead);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtil.responseCode(out,ResponseCode.SERVER_ERROR);
            return;
        }
        ResponseUtil.responseCode(out,code);

    }





}
